package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.DealMapper;
import com.example.finalsystem.Dao.ListGoodsMapper;
import com.example.finalsystem.Entity.Client;
import com.example.finalsystem.Entity.Deal;
import com.example.finalsystem.Entity.ListGoods;
import com.example.finalsystem.Entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class DealService {
    @Autowired
    MemberService memberService;
    @Autowired
    ClientService clientService;
    @Autowired
    ListGoodsService listGoodsService;
    @Autowired
    OutListService outListService;


    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = DealService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }

    public List<Deal> findDealByStaffId(String userId,SqlSession sqlSession)
    {
        List<Deal> deals = sqlSession.getMapper(DealMapper.class).findDealByStaffId(userId);
//        sqlSession.close();
        return deals;
    }
    public List<Deal> findDealByClientId(Long clientId)
    {
        SqlSession sqlSession = openSession();
        List<Deal> deals = sqlSession.getMapper(DealMapper.class).findDealByClientId(clientId);
        sqlSession.close();
        return deals;
    }
//    Deal 中的人物一定要已存在
    public String addDeal(String staffId,Long clientId,String content,Date signTime){
        SqlSession sqlSession = openSession();
        User user =  memberService.getPerson(staffId);
        Client client = clientService.getClient(clientId);
        String str =""+(System.currentTimeMillis()/1000);
        Deal deal = new Deal();
        deal.setFkStaffid(staffId);
        deal.setFkStaffname(user.getUsername());
        deal.setFkClientid(clientId);
        deal.setFkClientname(client.getClientname());
        deal.setDealtime(new Date());
        deal.setSigntime(signTime);
        deal.setDealcontent(content);
        deal.setDealid(str.substring(str.length()-6));
        sqlSession.getMapper(DealMapper.class).insert(deal);
        sqlSession.commit();
        sqlSession.close();
        return deal.getDealid();
    }
    public boolean canFix(String dealId,SqlSession sqlSession)
    {
//        SqlSession sqlSession = openSession();
        List<ListGoods> listGoods = listGoodsService.getListGoodsByDealId(dealId,sqlSession);
//        如果合同对应的货物清单有发货单，则不可以修改
        for (ListGoods g:listGoods
             ) {
            if (outListService.hasFK_listGoodsId(g.getListgoodsid(),sqlSession))
            {
                return  false   ;
            }
        }

        return true;
    }
    public String fixDeal(String dealId ,String staffId,Long clientId,String content,Date signTime){
        SqlSession sqlSession = openSession();
        User user =  memberService.getPerson(staffId);
        Client client = clientService.getClient(clientId);
        Deal deal = sqlSession.getMapper(DealMapper.class).selectByPrimaryKey(dealId);
        deal.setDealcontent(content);
        deal.setFkClientid(clientId);
        deal.setFkStaffid(staffId);
        deal.setSigntime(signTime);
        deal.setFkClientname(client.getClientname());
        deal.setFkStaffname(user.getUsername());
        sqlSession.getMapper(DealMapper.class).updateByPrimaryKeyWithBLOBs(deal);
        sqlSession.commit();
        sqlSession.close();
        return "修改成功";
    }
    public String deleteDeal(String dealId){
        SqlSession sqlSession = openSession();

        Deal deal= sqlSession.getMapper(DealMapper.class).selectByPrimaryKey(dealId);
        sqlSession.getMapper(DealMapper.class).deleteByPrimaryKey(dealId);
        sqlSession.getMapper(ListGoodsMapper.class).deleteAllByFK_dealId(dealId);
        sqlSession.commit();
        sqlSession.close();
        return "删除成功";
    }
    public Deal getDeal(String dealId){
        SqlSession sqlSession = openSession();
        Deal deal = sqlSession.getMapper(DealMapper.class).selectByPrimaryKey(dealId);
        sqlSession.close();
        return deal;
    }
    public List<Deal> getListOfDeal(int currentPage , int pageSize)
    {
        SqlSession sqlSession =openSession();
        DealMapper dealMapper = sqlSession.getMapper(DealMapper.class);
        List<Deal> deals =dealMapper.getListOfDeal(currentPage,pageSize);
        sqlSession.close();
        return deals;
    }
    public int countDeal()
    {
        SqlSession sqlSession =openSession();
        DealMapper dealMapper = sqlSession.getMapper(DealMapper.class);
        sqlSession.close();
        return dealMapper.countDeal();
    }
    public List<Deal> getAllDeal(SqlSession sqlSession)
    {
        List<Deal> list = sqlSession.getMapper(DealMapper.class).findAllDeal();
//        sqlSession.close();
        return list ;
    }
}
